#include <stdio.h>
#include "io_led.h"

static inline int io_led_set(void* self, int io_level)
{
    return (*(IoLed_i**)self)->io_set(self, io_level);
}
static inline int io_led_read(void* self, int *io_level)
{
    return (*(IoLed_i**)self)->io_read(self, io_level);
}

int io_led_on(IoLed *self)
{
    PRINT_FILE_LINE_FUNC();
    io_led_set(self->io_dev, LIGHT_ON);
    return 0;
}
int io_led_off(IoLed *self)
{
    PRINT_FILE_LINE_FUNC();
    return io_led_set(self->io_dev, LIGHT_OFF);
}

int io_led_get_state(IoLed *self, int *state)
{
    int temp_io_level;
    PRINT_FILE_LINE_FUNC();
    if (io_led_read(self->io_dev, &temp_io_level))
        return -1;
    
    *state = temp_io_level ? LIGHT_ON : LIGHT_OFF;
    // if(temp_io_level) {
    //     *state = LIGHT_ON;
    // }
    // else {
    //     *state = LIGHT_OFF;
    // }

    return 0;
}

Light_i io_led_interface = {
    (light_on_fn_t)io_led_on, 
    (light_off_fn_t)io_led_off, 
    (light_get_state_fn_t)io_led_get_state
};

int io_led_init(IoLed *self, IoLed_i** io_dev)
{
    PRINT_FILE_LINE_FUNC();
    self->interface = &io_led_interface;
    self->io_dev = io_dev;
    self->state = LIGHT_OFF;
    return 0;
}
